package com.example.springbotorderrabbitmqproducer.service;

import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class OrderService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void makeOrder(String userId, String productId, int num) {
        String orderId = UUID.randomUUID().toString();

        System.out.println("订单生成成功！");
        String exchangeName = "fanout-order-exchange";
        String routingKey = "";

        //参数1 交换机，参数2 路由key/queue队列名称 参数3 消息内容
        rabbitTemplate.convertAndSend(exchangeName, routingKey, orderId);
    }

    public void makeDirectOrder(String userId, String productId, int num) {
        String orderId = UUID.randomUUID().toString();

        System.out.println("订单生成成功！");
        String exchangeName = "direct-order-exchange";
        String routingKey = "sms";

        //参数1 交换机，参数2 路由key/queue队列名称 参数3 消息内容
        rabbitTemplate.convertAndSend(exchangeName, routingKey, orderId);
    }

    public void makeTopicOrder(String s, String s1, int i) {
        String orderId = UUID.randomUUID().toString();

        System.out.println("订单生成成功！");
        String exchangeName = "topic-order-exchange";
        String routingKey = "com.sms.";

        //参数1 交换机，参数2 路由key/queue队列名称 参数3 消息内容
        rabbitTemplate.convertAndSend(exchangeName, routingKey, orderId);
    }

    public void makeTTLOrder(String s, String s1, int i) {
        String orderId = UUID.randomUUID().toString();

        System.out.println("订单生成成功！");
        String exchangeName = "ttl-direct-exchange";
        String routingKey = "ttl";

        //参数1 交换机，参数2 路由key/queue队列名称 参数3 消息内容
        rabbitTemplate.convertAndSend(exchangeName, routingKey, orderId);
    }

    public void makeLimitOrder(String s, String s1, int i) {
        String orderId = UUID.randomUUID().toString();

        System.out.println("订单生成成功！");
        String exchangeName = "lim-fanout-exchange";
        String routingKey = "";

        //参数1 交换机，参数2 路由key/queue队列名称 参数3 消息内容
        rabbitTemplate.convertAndSend(exchangeName, routingKey, orderId);
    }

    public void makeTTLMessageOrder(String s, String s1, int i) {
        String orderId = UUID.randomUUID().toString();

        System.out.println("订单生成成功！");
        String exchangeName = "ttl-direct-exchange";
        String routingKey = "message";

        MessagePostProcessor messagePostProcessor = new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {

                message.getMessageProperties().setExpiration("5000");
                message.getMessageProperties().setContentEncoding("UTF-8");

                return message;
            }
        };

        //参数1 交换机，参数2 路由key/queue队列名称 参数3 消息内容
        rabbitTemplate.convertAndSend(exchangeName, routingKey, orderId, messagePostProcessor);
    }

}
